package com.xyhuo.leetcode.y2025;

import com.xyhuo.leetcode.common.TreeNode;

import java.util.ArrayList;
import java.util.List;

public class BinaryTreePaths {

    public List<String> binaryTreePaths(TreeNode root) {
        ArrayList<Integer> list = new ArrayList<>();
        search(root, list);
        return ans;
    }

    private List<String> ans = new ArrayList<>();

    private void search(TreeNode root, List<Integer> list) {
        if (root == null) {
            return;
        }
        list.add(root.val);
        search(root.left, list);
        search(root.right, list);
        if (root.left == null && root.right == null) {
            ans.add(getStr(list));
        }
        list.remove(list.size() - 1);
    }

    private String getStr(List<Integer> list) {
        StringBuilder sb = new StringBuilder();
        sb.append(list.get(0));
        for (int i = 1; i < list.size(); i++) {
            sb.append("->");
            sb.append(list.get(i));
        }
        return sb.toString();
    }
}
